import matplotlib.pyplot as plt

def parse_clk_line(line):
    parts = line.split(',')
    week = int(parts[1])
    tow = float(parts[2])
    stat = int(parts[3])
    dtr = [float(parts[i]) for i in range(5, 11)]
    return tow, dtr

def read_clk_file(filename):
    tows = []
    dtrs = [[] for _ in range(6)]
    
    with open(filename, 'r') as file:
        for line in file:
            if line.startswith('$CLK'):
                tow, dtr = parse_clk_line(line)
                tows.append(tow)
                for i in range(6):
                    dtrs[i].append(dtr[i])
    
    return tows, dtrs

def plot_dtr(tows, dtrs, filename):
    fig, axs = plt.subplots(6, 1, figsize=(10, 15), sharex=True)
    
    for i in range(6):
        axs[i].plot(tows, dtrs[i], label=f'dtr[{i}] * 1E9')
        axs[i].set_ylabel(f'dtr[{i}] * 1E9')
        axs[i].legend()
        axs[i].grid(True)
    
    axs[-1].set_xlabel('Time of Week (tow)')
    
    # Split title into two lines if it's too long
    max_title_length = 100
    title = filename
    if len(title) > max_title_length:
        title = title[:max_title_length] + '\n' + title[max_title_length:]
    
    fig.suptitle(title)  # 添加图片标题为文件名
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    AAA = 1
    print("AAA hello world= ", AAA)
    #filename = r'E:\fieldTest\ublox-F9P\test09-241215nigth152656\COM13_241215_152656_winvscode_RlsKinema_GOOD.pos.stat'  # 替换为你的文件名
    #filename = r'E:\fieldTest\ublox-F9P\test09-241215nigth152656\COM13_241215_152656_winvscode_RlsSingle_GOOD.pos.stat'  

    # filename = r"E:\fieldTest\ublox-f9r\data\20250120_night\20250120_115038D1_SecondField\COM13___9600_250120_115032P_rtk.pos.stat"
    # filename = r"E:\fieldTest\ublox-f9r\data\20250120_night\20250120_115038D1_SecondField\COM13___9600_250120_115032P_single.pos.stat"
    
    filename = r"E:\fieldTest\ublox-f9r\data\20250120_night\20250120_135505Dopen_xihuBrige\COM13___9600_250120_135519PopenD_rtk.pos.stat"
    #filename = r"E:\fieldTest\ublox-f9r\data\20250120_night\20250120_135505Dopen_xihuBrige\COM13___9600_250120_135519PopenD_single.pos.stat"
    tows, dtrs = read_clk_file(filename)
    plot_dtr(tows, dtrs, filename)
